$(function () {
    Time();
    load();
})

function fh() {
    location.href = "../../tan_kai/Dfcsp.html";
    $(".ad").removeAttr("hidden");
    $("#XQ").removeAttr("hidden");
    Time();
    load();
}

function Time() {
    var time = new Date();
    var y = time.getFullYear();
    var mon = time.getMonth() + 1; //0-11
    var d = time.getDate();
    var h = time.getHours();
    var m = time.getMinutes();
    var s = time.getSeconds();
    $("#datetime").val(y + "-" + mon + "-" + d + " " + h + ":" + m + ":" + s);
}

function load() {
    $("#submit").prop("hidden", true)
    let u = localStorage.getItem("user");
    let user = JSON.parse(u);
    $("#name").val(user.rname);
    let DfileOnt = JSON.parse(localStorage.getItem("DfileOnt"));
    $("#productname").val(DfileOnt.productname);
    $("#productid").val(DfileOnt.productid);

    let str = " <tr >\n" +
        "           <th>点选</th>\n" +
        "           <th>物料名称</th>\n" +
        "           <th>物料编号</th>\n" +
        "           <th>用途类型</th>\n" +
        "           <th>数量</th>\n" +
        "           <th>单位</th>\n" +
        "           <th>单价(元)</th>\n" +
        "           <th>小计(元)</th>\n" +
        "       </tr>\n";
    let list;
    if (localStorage.getItem("DMlist") != null) {
        list = JSON.parse(localStorage.getItem("DMlist"));
        $.each(list, function (index, item) {
            str +=
                "  <tr>\n" +
                "      <td><input type=\"checkbox\" name='checkbox' value='" + item.productid + "'></td>\n" +
                "      <td>" + item.productname + "</td>\n" +
                "      <td>" + item.productid + "</td>\n" +
                "      <td>" +(item.type=='Y001-1'?'商品':item.type=='Y001-2'?'物料':'') + "</td>\n" +
                "      <td><input type=\"number\" value='0' class=\"amount\"  style=\"border: none;height: 100%;width: 100%;\"></td>\n" +
                "      <td><span class='amountunit' >" + item.amountunit + "</span></td>\n" +
                "      <td><span class='realcostprice' >" + item.costprice + "</span></td>\n" +
                "      <td><span class='xiaoji'></span></td>\n" +
                "  </tr>";
        })
    }
    $("#tableAdd").html(str);
}

function Del() {
    let ids = [];
    $("[name='checkbox']:checked").each(function () {
        ids.push($(this).val());
    })
    if (ids.length > 1 || ids.length ==0) {
        return;
    }else {
        let ls = JSON.parse(localStorage.getItem("DMlist"));
        let list = [];
        let js = 0;
        for (let i = 0; i < ids.length; i++) {
            $.each(ls, function (index, item) {
                if (ids[i] != item.productid) {
                    list[js]=item;
                    js=js+1;
                }
            })
        }
        localStorage.setItem("DMlist", JSON.stringify(list));
        load();
    }
}

function add() {
    $("#AccRoleModal").modal("show");
    page();
    loadpage();
}

function page() {
    $("#tm01").bootstrapTable({
        //请求方式  get请求中文会乱码，建议使用post请求
        method: 'post',
        //请求头部的设置
        contentType: 'application/json',
        //url
        url: '/Dflile.do/pagewl',
        //设置交替行样式
        striped: true,
        //初始显示页号
        pageNumber: 1,
        //设置表格高度
        //height: 350,
        //是否显示分页
        pagination: true,
        //设置是服务端分页还是客户端分页
        sidePagination: 'server',
        //页大小
        pageSize: 5,
        //分页列表
        pageList: [3, 5, 6, 10],
        //上传到服务端的参数
        queryParams: function (params) {
            return {
                num: params.offset / params.limit + 1,
                size: params.limit
            }
        },
        // 定义列名
        columns: [
            {title: "物料编号", field: "productid", align: "center"},
            {title: "物料名称", field: "productname", align: "center"},
            {title: "用途类型", field: "type", align: "center",
                formatter: function (vale, row, index) {
                    return vale == 'Y001-1' ? "商品" : vale == 'Y001-2' ? "物料" : ""
                }},
            {title: "物料描述", field: "productdescribe", align: "center"},
            {title: "单位", field: "amountunit", align: "center"},
            {title: "计划成本单价(元)", field: "costprice", align: "center"},
            {title: "操作", field: "productid", align: "center",
                formatter: function (vale, row, index) {
                    let ma = JSON.stringify(row);
                    let up_btn = "  <button class=\"btn btn-primary\" " +
                        " onclick='maAdd(" + ma + ")'>添加</button>";
                    return up_btn;
                }
            }
        ]
    })
}

// 刷新页面
function loadpage() {
    $("#tm01").bootstrapTable("refresh")
}

function maAdd(ma) {
    let list = [];
    if (localStorage.getItem("DMlist") == null) {
        list.push(ma);
        localStorage.setItem("DMlist", JSON.stringify(list));
        load();
    } else {
        let ls = JSON.parse(localStorage.getItem("DMlist"));
        list = ls;
        for (let i = 0; i < ls.length; i++) {
            if (ls[i].productid == ma.productid) {
                return;
            }
        }
        list[list.length] = ma;
        localStorage.setItem("DMlist", JSON.stringify(list));
        load();
    }
}

function yl() {
    $("#submit").removeAttr("hidden");
    $("#XQ").prop("hidden", true);
    $(".ad").prop("hidden", true);

    let shu = [];
    $(".amount").each(function () {
        shu.push($(this).val());
    })
    let dj = [];
    $(".realcostprice").each(function () {
        dj.push($(this).text());
    })
    let s = 0;
    $(".xiaoji").each(function () {
        $(this).text(parseInt(shu[s]) * parseInt(dj[s]))
        s = s + 1;
    })
}

function sut() {
    let lists = [];

    let shu = [];
    $(".amount").each(function () {
        shu.push($(this).val());
    })
    for (let i = 0; i < shu.length; i++) {
        if (parseInt(shu[i])<=0) {
            alert("数量不能为小于等于0！")
            return;
        }
    }
    let xiaoji = [];
    $(".xiaoji").each(function () {
        xiaoji.push($(this).text());
    })
    let list = JSON.parse(localStorage.getItem("DMlist"));
    let user = JSON.parse(localStorage.getItem("user"));
    
    for (let i = 0; i < list.length; i++) {
        let Mapply = {
            "productid": list[i].productid,
            "productname": list[i].productname,
            "firstkindid": list[i].firstkindid,
            "firstkindname": list[i].firstkindname,
            "secondkindid": list[i].secondkindid,
            "secondkindname": list[i].secondkindname,
            "thirdkindid":list[i].thirdkindid,
            "thirdkindname": list[i].thirdkindname,
            "designer": $("#sjr").val(),
            "moduledescribe": $("#remark").val(),
            "costpricesum": xiaoji[i],
            "register": $("#name").val(),
            "registertime": $("#datetime").val(),
            "checktag": "S001-0",
            "changetag": "B002-0",
            "amount":shu[i]
        };
        lists[i] = Mapply;
    }
    console.log(lists)
    let str = JSON.stringify(lists);
    let plist = localStorage.getItem("DfileOnt");
    $.post("/Dmodule.do/DmoduleAdd", {lists: str,plist:plist}, function (sdate) {
        $("#staticBackdrop").modal("show");
        localStorage.removeItem("DMlist")
    })
}
function hh() {
    location.href = "../../tan_kai/Dfcsp.html";
}







